package com.metaera.app.ui.fragments;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.metaera.app.R;
import com.metaera.app.ui.activities.SearchActivity;
import com.metaera.app.ui.adapters.SimpleFragmentStatePagerAdapter;
import com.metaera.app.ui.adapters.navigator.FlashNewsNavigatorAdapter;
import com.metaera.app.ui.core.BaseFragment;
import com.metaera.app.utils.ViewUtils;
import com.metaera.app.utils.event.OnLauncherCalendarEvent;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.viewpager.widget.ViewPager;

/**
 * 快讯
 */
public class FlashNewsFragment extends BaseFragment implements View.OnClickListener {

    private ViewPager      mViewPager;
    private MagicIndicator mMagicIndicator;

    private List<String> mDataList = Arrays.asList("快讯", "日历");

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_quick;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ViewUtils.addStatusBarHeightPadding(findViewById(R.id.layout_container));

        // find
        mViewPager = findViewById(R.id.flash_news_view_pager);
        mMagicIndicator = findViewById(R.id.magic_indicator_flash_news);

        // init listener
        findViewById(R.id.img_right_add_title).setOnClickListener(this);

        // init view
        CommonNavigator commonNavigator = new CommonNavigator(mViewPager.getContext());
        commonNavigator.setAdapter(new FlashNewsNavigatorAdapter(mViewPager, mDataList));

        mMagicIndicator.setNavigator(commonNavigator);
        mViewPager.setAdapter(new SimpleFragmentStatePagerAdapter(getChildFragmentManager(),
                new ArrayList<>(Arrays.asList(new FlashNewsTypeFragment(), new FlashNewsCalendarTypeFragment()))));

        ViewPagerHelper.bind(mMagicIndicator, mViewPager);

        // 日历
        Bundle arguments = getArguments();
        if (arguments != null){
            mViewPager.setCurrentItem(arguments.getInt("index"));
        }
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.img_right_add_title) {
            startActivity(SearchActivity.class);
        }
    }

    @Subscribe(sticky = true)
    public void event(OnLauncherCalendarEvent event){
        if (mViewPager != null){
            mViewPager.setCurrentItem(1); // 日历
        }
    }

}
